﻿Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports System.Data.Common
Public Class RolData
    Inherits ClaseBaseData
#Region "Declaracion"
    Private _database As Database
#End Region

#Region "Constructor"
    Public Sub New()
        _database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("Vinum")
    End Sub
#End Region

    Public Function Insertar(ByVal Rol As RolObj) As RolObj
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaInsertarRoles")
                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "Descripcion", DbType.String, Rol.Descripcion)

                _database.AddOutParameter(Comando, "Id", DbType.Int32, 100)

                _database.ExecuteNonQuery(Comando)

                Rol.Id = Convert.ToInt32(_database.GetParameterValue(Comando, "Id"))

                Transaccion.Complete()

                Return Rol

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Function

    Public Sub Actualizar(ByVal Rol As RolObj)
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaActualizarRoles")
                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "Id", DbType.Int32, Rol.Id)
                _database.AddInParameter(Comando, "Descripcion", DbType.String, Rol.Descripcion)

                _database.ExecuteNonQuery(Comando)

                Transaccion.Complete()

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Sub

    Public Sub Eliminar(ByVal Rol As RolObj)
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaEliminarRoles")
                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "Id", DbType.Int32, Rol.Id)

                _database.ExecuteNonQuery(Comando)

                'TODO:
                Transaccion.Complete()

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Sub

    Public Overrides Function Load(ByVal Dr As IDataReader) As Object
        Dim Rol As RolObj = New RolObj
        Try
            Rol.Id = Dr.GetInt32(Dr.GetOrdinal("Codigo"))
            Rol.Descripcion = Dr.GetString(Dr.GetOrdinal("Descripcion"))
        Catch ex As Exception
            Throw ex
        End Try
        Return Rol
    End Function

    Public Overrides Function Buscar(ByVal _Obj As BuscarObj) As BindingList(Of Object)
        Dim Item As Object = Nothing
        Dim Coleccion As New BindingList(Of Object)

        Try
            Dim storedProcCommand As DbCommand = Me._database.GetStoredProcCommand(_Obj.ProcedimientoSQL)

            Me._database.AddInParameter(storedProcCommand, "Opcion", DbType.Int32, 0)
            Me._database.AddInParameter(storedProcCommand, "NombreDelCampo", DbType.String, _Obj.NombreCampoABuscar)
            Me._database.AddInParameter(storedProcCommand, "ValorABuscar", DbType.String, _Obj.TextoABuscar)
            Me._database.AddInParameter(storedProcCommand, "TipoCampo", DbType.Int32, _Obj.TipoDeCampoABuscar)

            Dim dr As IDataReader = Me._database.ExecuteReader(storedProcCommand)
            Using dr
                Do While dr.Read
                    Item = Me.Load(dr)
                    Coleccion.Add(Item)
                Loop
                Return Coleccion
            End Using
        Catch ex As Exception
            Throw
        End Try
        Return Coleccion
    End Function

End Class
